package com.zl.aop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * @author ZLL
 * @date create in 2018/05/09
 */
@Aspect
public class TestAnnotationAspect {

    @Pointcut(value = "execution(* com.zl.aop.service..*(..))")
    public void pointMethood(){

    }


    @Before(value = "pointMethood()")
    public void doBefore2(){
        System.out.println("doBefore2............");
    }


    @After(value = "pointMethood()")
    public void doAfter2(){
        System.out.println("doAfter2...........");
    }


    @Around(value = "pointMethood()")
    public void doAround(ProceedingJoinPoint pjp){

        System.out.println("around before...........");

        try {
            Object ret = pjp.proceed(pjp.getArgs());
            System.out.println("around ret ===>"+ret);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        System.out.println("around after ..............");
    }

    @AfterReturning(value = "pointMethood()", returning = "ret")
    public void doAfterReturning(JoinPoint joinPoint, String ret){
        System.out.println("doAfterReturning..................");
        String name = joinPoint.getTarget().getClass().getName();
        System.out.println(name);
        Object[] args = joinPoint.getArgs();
        System.out.println(JSON.toJSONString(args));

        Signature signature = joinPoint.getSignature();

        MethodSignature methodSignature = (MethodSignature) signature;

        Method method = methodSignature.getMethod();

        System.out.println(method.getName());
        System.out.println(method.getParameterTypes().length);
        System.out.println(method.getReturnType());

        System.out.println("doAfterReturning.................."+ret);
    }


    @AfterThrowing(value = "pointMethood()", throwing = "e")
    public void doAfterThrowing(Exception e){
        System.out.println("doAfterThrowing......."+e.getMessage());
    }
}
